//
//  RBGAppDelegate.m
//  BateauGenerique
//
//  Created by Kevin De Jesus Ferreira on 20/05/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGAppDelegate.h"
#import "RBGCreditsController.h"
#import "RBGHomeController.h"
#import "RBGContactController.h"
#import "RBGBoatOnDemand.h"
#import "RBGActualitesController.h"
#import "RBGAnnoncesController.h"
#import "RBGNavigationBar.h"
#import "RBGListeTypeAnnonceController.h"
#import "RBGFavorisController.h"
#import "RBGRangeViewController.h"
#import "RBGServiceChargement.h"

@implementation RBGAppDelegate

static float minValue = 0;
static float maxValue = 0;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    
    RBGSplashScreenController *controller = [[RBGSplashScreenController alloc] initWithNibName:@"RBGSplashScreenController" bundle:nil];
    
    controller.delegate = self;
    
    [navigationController pushViewController:controller animated:YES];
    
    [self enregistrementNotifications];
    
    return YES;
}

////////////////////////////NOTIFICATIONS/////////////////////////////
-(void)enregistrementNotifications{
    // Register for remote notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}
// We are registered, so now store the device token (as a string) on the AppDelegate instance
// taking care to remove the angle brackets first.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:
(NSData *)deviceToken {
    
    NSLog(@"My token is: %@", deviceToken);
    
    NSCharacterSet *angleBrackets = [NSCharacterSet characterSetWithCharactersInString:@"<>"];
    NSString *jeton = [[deviceToken description] stringByTrimmingCharactersInSet:angleBrackets];
    
    [[[RBGServiceChargement alloc] init] envoieJeton:jeton];
    
}
// Because toast alerts don't work when the app is running, the app handles them.
// This uses the userInfo in the payload to display a UIAlertView.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:
(NSDictionary *)userInfo {
    
    NSString *title = [(NSDictionary*)[(NSDictionary*)[userInfo objectForKey:@"aps"] objectForKey:@"alert"] objectForKey:@"body"];
    NSString *message = [(NSDictionary*)[userInfo objectForKey:@"aps"] objectForKey:@"message"];
    
    NSLog(@"%@", userInfo);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:
                          message delegate:nil cancelButtonTitle:
                          @"OK" otherButtonTitles:nil, nil];
    [alert show];
}
///////////////////////////////////////////////////////////////////////

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void) receiveDonnees:(Donnees *)donnees{
    
    self.donnees = donnees;
    
    /*
    self.donnees.parametres.backgroundColorUn = [UIColor blackColor];
    self.donnees.parametres.backgroundColorDeux = [UIColor orangeColor];
    self.donnees.parametres.fontColorDeux = [UIColor whiteColor];
     */
    self.donnees.parametres.fontColorUn = [UIColor orangeColor];

    UITabBarController * tabBar = [[UITabBarController alloc]init];
    
    RBGHomeController * home = [[RBGHomeController alloc]init];
    
    RBGCreditsController * credits = [[RBGCreditsController alloc]init];
    
    RBGContactController * contact = [[RBGContactController alloc]init];
    
    RBGBoatOnDemand * boatAndDemand = [[RBGBoatOnDemand alloc]init];
    
    RBGActualitesController * actualites = [[RBGActualitesController alloc]init];
    
    RBGListeTypeAnnonceController * annonces = [[RBGListeTypeAnnonceController alloc]init];
    
    RBGFavorisController * favoris = [[RBGFavorisController alloc]init];
    
    
    
    UIImage *original = [UIImage imageNamed: @"societe"];
    UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:5 orientation:original.imageOrientation];
    
    [home.tabBarItem setTitle:@"Société"];
    [home.tabBarItem setImage:small];
    
    [credits.tabBarItem setTitle:@"Crédits"];
    [credits.tabBarItem setImage:small];
    
    original = [UIImage imageNamed: @"contact"];
    small = [UIImage imageWithCGImage:original.CGImage scale:5 orientation:original.imageOrientation];
    
    [contact.tabBarItem setTitle:@"Contact"];
    [contact.tabBarItem setImage:small];
    
    original = [UIImage imageNamed: @"contact"];
    small = [UIImage imageWithCGImage:original.CGImage scale:5 orientation:original.imageOrientation];
    
    [boatAndDemand.tabBarItem setTitle:@"Ma recherche"];
    [boatAndDemand.tabBarItem setImage:small];
    
    original = [UIImage imageNamed: @"actualites"];
    small = [UIImage imageWithCGImage:original.CGImage scale:5 orientation:original.imageOrientation];
    
    [actualites.tabBarItem setTitle:@"Actualites"];
    [actualites.tabBarItem setImage:small];
    
    original = [UIImage imageNamed: @"annonces"];
    small = [UIImage imageWithCGImage:original.CGImage scale:5 orientation:original.imageOrientation];
    
    [annonces.tabBarItem setTitle:@"Annonces"];
    [annonces.tabBarItem setImage:small];
    
    original = [UIImage imageNamed: @"favoris"];
    small = [UIImage imageWithCGImage:original.CGImage scale:5 orientation:original.imageOrientation];
    
    [favoris.tabBarItem setTitle:@"Favoris"];
    [favoris.tabBarItem setImage:small];
    
    
    UINavigationController *navControllerHome = [[UINavigationController alloc] initWithRootViewController:home];
    UINavigationController *navControllerContact = [[UINavigationController alloc] initWithRootViewController:contact];
    
    
    
    UINavigationController *navControllerActualites = [[UINavigationController alloc] initWithRootViewController:actualites];
    
    UINavigationController *navControllerAnnonces = [[UINavigationController alloc] initWithRootViewController:annonces];
    
    UINavigationController *navControllerFavoris = [[UINavigationController alloc] initWithRootViewController:favoris];
    
    UINavigationController *navControllerBoatAndDemand = [[UINavigationController alloc] initWithRootViewController:boatAndDemand];
    
    UINavigationController *navControllerCredits = [[UINavigationController alloc] initWithRootViewController:credits];
    
    
    [tabBar setViewControllers:[NSArray arrayWithObjects:navControllerHome,navControllerAnnonces,navControllerActualites,navControllerContact,navControllerFavoris,navControllerBoatAndDemand,navControllerCredits,nil]];
    
    [[UITabBar appearance] setTintColor:self.donnees.parametres.backgroundColorDeux];
    
    original = [UIImage imageNamed: @"autres"];
    small = [UIImage imageWithCGImage:original.CGImage scale:6 orientation:original.imageOrientation];
    
    tabBar.moreNavigationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Autres" image:small tag:0];
    
    UITableView *view = (UITableView *)tabBar.moreNavigationController.topViewController.view;
    
    [tabBar.moreNavigationController.topViewController.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"fond"]]];
    
    tabBar.moreNavigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    [tabBar.moreNavigationController.navigationBar setBackgroundColor:[UIColor blackColor]];
    [tabBar.moreNavigationController.navigationBar setTintColor:self.donnees.parametres.fontColorUn];
   
    [tabBar.moreNavigationController.navigationBar.topItem setTitle:@"Autres"];
    [tabBar.moreNavigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : self.donnees.parametres.fontColorUn}];
    tabBar.moreNavigationController.navigationBar.translucent = NO;
    [tabBar.moreNavigationController.navigationBar.topItem.rightBarButtonItem setTitle:@""];
    [tabBar.moreNavigationController.navigationBar.topItem.rightBarButtonItem setEnabled:NO];

    tabBar.moreNavigationController.topViewController.navigationController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Retour"
                                                                             style:UIBarButtonItemStyleBordered
                                                                            target:nil
                                                                            action:nil];
    
    // Custom les cellules de la vue more
    if ([[view subviews] count]) {
        for (UITableViewCell *cell in [view visibleCells]) {
            cell.backgroundColor = [UIColor clearColor];
            cell.textLabel.textColor =self.donnees.parametres.fontColorDeux;
            UIView *selectedView = [[UIView alloc]init];
            selectedView.backgroundColor = self.donnees.parametres.backgroundColorDeux;
            cell.selectedBackgroundView =  selectedView;
        }
    }
    
    // Change la couleur des images dans la vue more
    [view setTintColor:self.donnees.parametres.fontColorDeux];
    
    //[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:@"fr", nil] forKey:@"AppleLanguages"]; // On met à jour la langue choisie fr -> pour la langue française
    //[[NSUserDefaults standardUserDefaults] synchronize];

    [self.window setRootViewController:tabBar];
    
    //light status bar
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    
}

- (void) receiveErreur:(int)statut{
    
}


+ (float)minValue {
    return minValue;
}

+ (float)maxValue {
    return maxValue;
}

+(void)setMinValue:(float) minVal{
    minValue = minVal;
}

+(void)setMaxValue:(float) maxVal{
    maxValue = maxVal;
}

-(void) effacerDonnees{
    self.categorie_id = nil;
    self.region_id = nil;
    self.marque_id = nil;
    self.modele_id = nil;
    self.prix_min_range = nil;
    self.prix_max_range = nil;
    self.longueur_min_range = nil;
    self.longueur_max_range = nil;
    self.puissance_min_range = nil;
    self.puissance_max_range = nil;
}

@end
